package com.github.kuangcp.graduate.sse.vo;

import org.springframework.util.StringUtils;

import java.util.Objects;

/**
 * https://www.cnblogs.com/goloving/p/9196066.html
 *
 * @author kuangcp
 */
public class MsgVO {

    public static final String COMMENT = new MsgVO(":\n\n").buildMsg();

    private String data;
    private String id;
    private Long retry;
    private String event;

    public String buildMsg() {
        StringBuilder builder = new StringBuilder();
        if (StringUtils.hasText(data)) {
            builder.append("data: ").append(data).append("\n");
        }
        if (StringUtils.hasText(id)) {
            builder.append("id: ").append(id).append("\n");
        }
        if (StringUtils.hasText(event)) {
            builder.append("event: ").append(event).append("\n");
        }
        if (Objects.nonNull(retry)) {
            builder.append("retry: ").append(retry).append("\n");
        }
        builder.append("\n\n");
        return builder.toString();
    }

    public MsgVO(String data) {
        this.data = data;
    }

    public MsgVO(String id, String data) {
        this.data = data;
        this.id = id;
    }

    public MsgVO(String id, Long retry) {
        this.id = id;
        this.retry = retry;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Long getRetry() {
        return retry;
    }

    public void setRetry(Long retry) {
        this.retry = retry;
    }

    public String getEvent() {
        return event;
    }

    public void setEvent(String event) {
        this.event = event;
    }
}
